<template>
	<view class="password">
		<u-toast ref="uToast"></u-toast>
		<view class="input ">
			<view class="flex flex_align">
				<!-- <text class="iconfont icon-shouji1" style="color: #beebff;margin-right: 10rpx;font-size: 40rpx;"></text> -->
				<u-input placeholder="请输入手机号码" prefixIcon="phone" border="bottom" v-model="addForm.mobile" placeholder-style="color:#bdbdbd" ></u-input>
			</view>
		</view>
		<view class="input mar_top">
			<u-input v-model="addForm.captcha" border="bottom" placeholder="请输入验证码" placeholder-style="color:#bdbdbd">
				<template slot="suffix">
					<u-code ref="uCode" @change="codeChange" seconds="60" ></u-code>
					<view style="padding: 5rpx 15rpx;color: #2366d5;font-size: 28rpx;" @tap="getCode">
						{{tips}}
					</view>
				<!-- 	<u-button @tap="getCode" :text="tips" size="small" :customStyle="{border: 'none',color: '#3c9cff'}"></u-button> -->
				</template>
			</u-input>
		</view>
	</view>
</template>

<script>
	import {
		send,
		mobilelogin
	} from '@/api/login.js'
	export default {
		data() {
			return {
				tips: '',
				addForm: {
					mobile: '',
					captcha: '',
					platform: 2
				}
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				if (this.addForm.mobile) {
					if (this.$refs.uCode.canGetCode) {
						uni.showLoading({
							title: '正在获取验证码',
							mask: true
						})
						send({
							mobile: this.addForm.mobile
						}).then(res => {
							uni.hideLoading();
							this.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						}).catch(err => {
							this.$u.toast(err.msg);
						})
					} else {
						this.$u.toast('倒计时结束后再发送');
					}
				} else {
					this.$u.toast('请先填写手机号码');
				}
			},
			//验证码登录
			getmobilelogin() {
				let addForm = this.addForm
				mobilelogin(addForm).then(res => {
					uni.setStorageSync("token", res.data.token)
					this.$store.commit('editToken',res.data.token)
					
					this.$store.commit('editMobile',res.data.mobile)
					uni.setStorageSync("mobile", res.data.mobile)
					
					uni.setStorageSync("expiretime", res.data.expiretime)
					this.$refs.uToast.show({
						message: '登录成功',
						icon: false,
						type: 'success', 
						complete: function() {
							uni.switchTab({
								url: '/pages/index/index'
							});
						}
					})
				}).catch(err => {
					this.$refs.uToast.show({
						message: err.msg,
						type: 'error',
						icon: false
					})
				})
			}
		}
	}
</script>

<style>
	/deep/.u-button--small {
		padding: 0 15px;
	}
</style>
<style scoped lang="scss">
	.password {
		.input {
			// background-color: #89dbff;
			border-radius: 16rpx;
			color: #fff;
			padding: 16rpx 20rpx;
		}

		.mar_tops {
			margin-top: 40rpx;
		}
	}
</style>
